package com.accp.entity;

import lombok.Data;
import me.chanjar.weixin.common.util.StringUtils;
import org.apache.activemq.ScheduledMessage;
import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.core.MessagePostProcessor;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 * Created by 尚晓忠 on 2018\7\22 0022 15:25:03
 */
@Data
public class ScheduleMessagePostProcessor   implements MessageCreator {
    private long delay = 0l;

    private String corn = null;
    private String context=null;
    public ScheduleMessagePostProcessor(long delay,String context) {
        this.delay = delay;
        this.context=context;
    }

    public ScheduleMessagePostProcessor(String cron,String context) {
        this.corn = cron;
        this.context=context;
    }

    @Override
    public Message createMessage(Session session) throws JMSException {
        TextMessage message = session.createTextMessage(context);
        if (delay > 0) {
            message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay);
        }
        if (!StringUtils.isEmpty(corn)) {
            message.setStringProperty(ScheduledMessage.AMQ_SCHEDULED_CRON, corn);
        }
        return message;
    }

}
